/*
global_world_proc.dm
This is mostly just world/New()
Honestly, it does such a large amount I don't even bother to mess with it.
*/

world
	New()
		..()
		//log=file("log.txt")
		var/ini_reader/IRead = new("sql_info.ini", INIREADER_INI)
		var/list/info = IRead.ReadSetting("SQL")
		sql_db   =info["db"]
		sql_ip   =info["ip"]
		sql_user =info["user"]	//sql variables
		sql_pass =info["pass"]
		sql_port =info["port"]
		sql_table=info["table"]
		sql_contentstable=info["ctable"]
		sql_objecttable  =info["otable"]
		sql_pstoretable  =info["store_table"]

		load_mysql()

		buildobjs-=/obj/builditems/portal
		buildobjs-=/obj/builditems/custom
		buildobjs-=/obj/builditems
		spawn() SortList(buildobjs)

proc
	load_mysql()
		if(MODIFY_TABLES || sql_ip == "localhost")
			modify_tables()
		if(LOAD_WORLD || sql_ip == "localhost")
			load_world()
		if(LOAD_SHOPS || sql_ip == "localhost")
			load_shops()


	modify_tables()
		var/list/VARS=tmob.vars

								//this spits out everything you need to set up the sql table
								//for characters
		for(var/x=1 to VARS.len)
			if(ignored_vars.Find(VARS[x]))continue
			if(istext(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_table]` ADD `[VARS[x]]` TEXT(300);")
				continue
			else if(isnum(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_table]` ADD `[VARS[x]]` INT(30);")
				continue
								//making sure each variable gets a proper type to suit it's
								//purpose
			else
				Query("ALTER TABLE `[sql_table]` ADD `[VARS[x]]` VARCHAR(300);")

		VARS = tobj.vars+list("aname","builtowner","placeable","path","saveable","savez","savey","savex","uid","mesage","Tele_X","Tele_Y","Tele_Z","typepath")
		for(var/x=1 to VARS.len)
			if(istext(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_objecttable]` ADD `[VARS[x]]` TEXT(300);")
				continue
			else if(isnum(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_objecttable]` ADD `[VARS[x]]` INT(30);")
				continue
								//making sure each variable gets a proper type to suit it's
								//purpose
			else
				Query("ALTER TABLE `[sql_objecttable]` ADD `[VARS[x]]` VARCHAR(300);")

		VARS = stackvars
		for(var/x=1 to VARS.len)
			if(istext(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_contentstable]` ADD `[VARS[x]]` TEXT(300);")
				continue
			else if(isnum(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_contentstable]` ADD `[VARS[x]]` INT(30);")
				continue
								//making sure each variable gets a proper type to suit it's
								//purpose
			else
				Query("ALTER TABLE `[sql_contentstable]` ADD `[VARS[x]]` VARCHAR(300);")

		VARS = pstorevars
		for(var/x=1 to VARS.len)
			if(istext(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_pstoretable]` ADD `[VARS[x]]` TEXT(300);")
				continue
			else if(isnum(VARS[VARS[x]]))
				Query("ALTER TABLE `[sql_pstoretable]` ADD `[VARS[x]]` INT(30);")
				continue
								//making sure each variable gets a proper type to suit it's
								//purpose
			else
				Query("ALTER TABLE `[sql_pstoretable]` ADD `[VARS[x]]` VARCHAR(300);")

	load_world()
		var/list/ids=list()
		var/DBQuery/qry = Query("SELECT id FROM `[sql_objecttable]`;")
		if(qry.RowCount() > 0)
			while(qry.NextRow())
				var/list/row_data = qry.GetRowData()
				for(var/D in row_data)
					ids += row_data[D]

		for(var/x in ids)
			var/DBQuery/qry_ld = Query("SELECT * FROM `[sql_objecttable]` WHERE id=[x];")
			if(qry_ld.RowCount() > 0)
				var/list/tvlist=list()
				while(qry_ld.NextRow())
					var/list/row_data = qry_ld.GetRowData()
					for(var/D in row_data)
						tvlist += D
						tvlist[D]=row_data[D]

		//var/obj/builditems/obb=new tvlist["typepath"]
				var/z = text2path(tvlist["typepath"])
				var/obj/builditems/ob=new z
				for(var/o in tvlist)
					if(ob.vars.Find(o) && (o == "layer" || (!(o in ignored_vars))) )
						if(oddvars.Find(o))ob.vars[o]=text2num(tvlist[o])
						else ob.vars[o]=tvlist[o]
				ob.loc=locate(text2num(tvlist["x"]),text2num(tvlist["y"]),text2num(tvlist["z"]))

				if(ob.iconFile)
					ob.icon = icon(file(ob.iconFile))
				else
					ob.icon='BuildTwo.dmi'
				ob.name=tvlist["name"]
				var/obj/builditems/check = locate(ob.type) in buildobjs
				if(check)
					ob.icon= check.icon

	load_shops()
		var/DBQuery/lqry = Query("SELECT * FROM `[sql_pstoretable]`;")
		if(lqry.RowCount() > 0)
			while(lqry.NextRow())
				var/list/row_data = lqry.GetRowData()
				var/type = text2path(row_data["type"])

				switch(row_data["type"])
					if("/obj/shop_related/player_shop/counter")
						new type(locate(text2num(row_data["x"]),text2num(row_data["y"]),text2num(row_data["z"])), row_data["ckey"], row_data["uid"])

					if("/obj/shop_related/player_shop/money_claim_box")
						var/obj/shop_related/player_shop/money_claim_box/mcb = new type(locate(text2num(row_data["x"]),text2num(row_data["y"]),text2num(row_data["z"])), row_data["ckey"], row_data["uid"])
						mcb.money = text2num(row_data["cur_money"])

					if("/obj/shop_related/player_shop/custom")
						var/obj/shop_related/player_shop/custom/c = new type(locate(text2num(row_data["x"]),text2num(row_data["y"]),text2num(row_data["z"])), row_data["ckey"], row_data["uid"])
						c.icon = icon(file(row_data["iconFile"]))
						c.icon_state = row_data["icon_state"]
						c.layer = 7
						c.cost = text2num(row_data["cost"])

					//gettable
					else
						var/obj/gettable/c = new type(locate(text2num(row_data["x"]),text2num(row_data["y"]),text2num(row_data["z"])), row_data["ckey"])
						c.is_for_sale = 1
						c.layer = 7
						for(var/obj/shop_related/player_shop/counter/cc in c.loc)
							cc.ref = src
							break
						c.uid = row_data["uid"]
						c.cost = (text2num(row_data["cost"]) ? text2num(row_data["cost"]) : 0)
						c.amount = text2num(row_data["amount"])